import argparse
import time

from libs import config
from libs.action import WebDriverAction
from libs.game import Game
from libs.image import ImageTool
from libs.log import Log
from libs.movement import Movement
from libs.trade_helper import TradeHelper
from libs.window import AdsWindow


if __name__ == "__main__":
    accounts = config.get_accounts()
    for user_id in accounts:
        if int(user_id) < 16:
            continue
        log = Log(user_id)
        driver = AdsWindow(user_id).open(False)
        game = Game(driver, user_id)
        action = WebDriverAction(driver)
        movement = Movement(driver)
        image_tool = ImageTool(driver)
        trade_helper = TradeHelper(driver, user_id)

        game.enter_game()

        # 需要买的东西
        items_to_buy = {
            "Copperite Bar": 1,
            "Copperite Ore": 3,
            "Silk Fiber": 6,
            "Silk Rope": 1,
            "Silk Slug Slime": 4,
            "Popberry Wine": 1,
            "Popberry": 24,
            "Cooking Mix": 10,
            "Honey": 3,
            "Whittlewood Log": 3,
            "Whittlewood Plank": 1,
            "Grainbow": 2,
            "Grainbow Flour": 1,
            "Clayum Matrix": 2,
            "Clayum Powder": 1
        }

        # buy
        trade_helper.goto_buy_position()
        game.get_backpack()
        filter_input = trade_helper.open_market_buy()
        for item_name in items_to_buy:
            qty = items_to_buy.get(item_name)
            has_qty = game.get_backpack_qty(item_name)
            if qty > has_qty:
                trade_helper.market_buy_item(filter_input, item_name, qty - has_qty)
                action.escape()
        action.escape()
        action.escape()

